﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Cosmetic.DataEntity;

namespace Cosmetic.Logic
{
	/// <summary>
	/// Brand manager class
	/// </summary>
	public class BrandManager : LogicBase
	{
		private ProductSeriesManager m_SeriesManager	= new ProductSeriesManager();

		/// <summary>
		/// Insert a new Brand into database
		/// </summary>
		/// <param name="aBrand">new Brand</param>
		public void Insert(Brand aBrand)
		{
			m_CosmeticDC.Brands.InsertOnSubmit(aBrand);
			m_CosmeticDC.SubmitChanges();
		}

		/// <summary>
		/// Delete Brand
		/// </summary>
		/// <param name="aBrand">The Brand want delete</param>
		public void Delete(Brand aBrand)
		{
			List<ProductSeries> theSeriesList	= aBrand.ProductSeries.ToList();
			foreach (ProductSeries theSeries in theSeriesList)
			{
				this.m_SeriesManager.Delete(theSeries);
			}


			m_CosmeticDC.Brands.DeleteOnSubmit(aBrand);
			m_CosmeticDC.SubmitChanges();
		}





		/// <summary>
		/// check the aBrand whether or not already exists.
		/// </summary>
		/// <param name="aEnglishName">brand english name</param>
		/// <param name="aChineseName">brand chinese name</param>
		/// <returns>if exists return true else return false</returns>
		public bool CheckExists(string aEnglishName, string aChineseName)
		{
			List<Brand> theBrandList	= (from theBrand in m_CosmeticDC.Brands
											  where theBrand.EnglishName == aEnglishName && theBrand.ChineseName == aChineseName
											  select theBrand).ToList();
			return theBrandList.Count > 0;
		}
		/// <summary>
		/// check the aBrand whether or not already exists.
		/// </summary>
		/// <param name="aBrand">the brand wanna check</param>
		/// <returns>if exists return true else return false</returns>
		public bool CheckExists(Brand aBrand)
		{
			List<Brand> theBrandList	= (from theBrand in m_CosmeticDC.Brands
											  where theBrand.EnglishName == aBrand.EnglishName 
											  && theBrand.ChineseName == aBrand.ChineseName
											  && theBrand.BrandId != aBrand.BrandId
											  select theBrand).ToList();
			return theBrandList.Count > 0;
		}

		/// <summary>
		/// check the Brand whether or not Contains the Series.
		/// </summary>
		/// <param name="aBrand">brand</param>
		/// <param name="aSeriesName">want check the Series name</param>
		/// <returns>if contians return true else return false</returns>
		public bool CheckContains(Brand aBrand, string aSeriesName)
		{
			return this.CheckContains(aBrand.BrandId, aSeriesName);
		}
		/// <summary>
		/// check the Brand whether or not Contains the Series.
		/// </summary>
		/// <param name="aBrandId">brand id</param>
		/// <param name="aSeriesName">want check the Series name</param>
		/// <returns>if contians return true else return false</returns>
		public bool CheckContains(Guid aBrandId, string aSeriesName)
		{
			List<ProductSeries> theSeriesList	= (from theSeries in m_CosmeticDC.ProductSeries
												where theSeries.Name == aSeriesName && theSeries.BrandId == aBrandId
												select theSeries).ToList();
			return theSeriesList.Count > 0;
		}
		

		/// <summary>
		/// Get Brand by Brand Id
		/// </summary>
		/// <param name="aBrandId">Brand Id</param>
		/// <returns>Brand</returns>
		public Brand GetBrandById(Guid aBrandId)
		{
			return m_CosmeticDC.Brands.Single(theBrand => theBrand.BrandId == aBrandId);
		}

		/// <summary>
		/// get all brand
		/// </summary>
		/// <returns>all brand</returns>
		public List<Brand> GetAll()
		{
			return (from theBrand in m_CosmeticDC.Brands
				   orderby theBrand.EnglishName
				   select theBrand).ToList();
			
		}

		/// <summary>
		/// get brands by brand area 
		/// </summary>
		/// <param name="aBrandArea">brand area category</param>
		/// <returns>brand list</returns>
		public List<Brand> GetBrandByArea(Category aBrandArea)
		{
			return this.GetBrandByArea(aBrandArea.CategoryId);
		}
		/// <summary>
		/// get brands by brand area id
		/// </summary>
		/// <param name="aBrandAreaId">brand area category id</param>
		/// <returns>brand list</returns>
		public List<Brand> GetBrandByArea(Guid aBrandAreaId)
		{
			return (from theBrand in m_CosmeticDC.Brands
						where theBrand.BrandAreaCategoryId == aBrandAreaId
						orderby theBrand.EnglishName descending
						select theBrand).ToList();
		}

        public List<Brand> GetBrandByCategoryName(string name)
        {
            var brands = (from theBrand in m_CosmeticDC.Brands
                          where theBrand.BrandAreaCategoryId == (from category in m_CosmeticDC.Categories
                                                                 where category.Name == name
                                                                 select category.CategoryId).Single()
                          select theBrand).ToList();
            return brands;
        }

		/// <summary>
		/// get brands by brand english name initial letter from the all brands, 
		/// if want get no english name or only have chinese name brands, 
		/// please use anyother not letter char
		/// </summary>
		/// <param name="aLetter">the initial letter</param>
		/// <returns>brand list</returns>
		public List<Brand> GetBrandByInitial(string aLetter)
		{
			if (aLetter.ToLower()[0] >= 'a' && aLetter.ToLower()[0] <= 'z')
			//if (Char.IsLetter(aLetter, 0))
			{
				return (from theBrand in m_CosmeticDC.Brands
						where theBrand.EnglishName.ToLower()[0] == aLetter.ToLower()[0]
						orderby theBrand.Name
						select theBrand).ToList();
			}
			else
			{
				return (from theBrand in m_CosmeticDC.Brands
						where theBrand.EnglishName.ToLower()[0] < 'a' || theBrand.EnglishName.ToLower()[0] > 'z'
						orderby theBrand.Name
						select theBrand).ToList();
			}
		}
		/// <summary>
		/// get brands by brand english name initial letter from the special brands, 
		/// if want get no english name or only have chinese name brands, 
		/// please use anyother not letter char
		/// </summary>
		/// <param name="aLetter">the initial letter</param>
		/// <param name="aBrandList"></param>
		/// <returns>brand list</returns>
		public List<Brand> GetBrandByInitial(string aLetter, List<Brand> aBrandList)
		{
			if (Char.IsLetter(aLetter, 0))
			{
				return (from theBrand in aBrandList
						where theBrand.EnglishName.ToLower()[0] == aLetter.ToLower()[0]
						select theBrand).ToList();
			}
			else
			{
				return (from theBrand in aBrandList
						where theBrand.EnglishName.ToLower()[0] < 'a' || theBrand.EnglishName.ToLower()[0] > 'z'
						select theBrand).ToList();
			}
		}
		/// <summary>
		/// get brands by brand english name initial letter from the special brand area category, 
		/// if want get no english name or only have chinese name brands, 
		/// please use anyother not letter char
		/// </summary>
		/// <param name="aLetter">the initial letter</param>
		/// <param name="aBrandArea">brand area category</param>
		/// <returns>brand list</returns>
		public List<Brand> GetBrandByInitial(string aLetter, Category aBrandArea)
		{
			return this.GetBrandByInitial(aLetter, this.GetBrandByArea(aBrandArea));
		}
		/// <summary>
		/// get brands by brand english name initial letter from the special brand area category id, 
		/// if want get no english name or only have chinese name brands, 
		/// please use anyother not letter char
		/// </summary>
		/// <param name="aLetter">the initial letter</param>
		/// <param name="aBrandAreaId">brand area category id</param>
		/// <returns>brand list</returns>
		public List<Brand> GetBrandByInitial(string aLetter, Guid aBrandAreaId)
		{
			return this.GetBrandByInitial(aLetter, this.GetBrandByArea(aBrandAreaId));
		}
	}
}
